/****************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.     *
 *                                                              *
 * Copyright (C) 2009, Jun Li All rights reserved.              *
 * More about Jun{                                              *
 * Homepage: http://www.jpxx.org/                               *
 * Email   : lijun@jpxx.org                                     *
 * }                                                            *
 * SOFTWARE ENGINEERING COLLEGE OF SICHUAN UNIVERSITY           *
 *                                                              *
 ****************************************************************/
package org.jpxx.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.jpxx.web.project.LayerEnum;

/**
 * 
 * @author Jun Li lijun@jpxx.org (http://www.jpxx.org)
 * @version 1.0.0 $ org.jpxx.web.Constants.java $ $ Date: 2009-5-16 $
 */
public final class Constants {

	private static Logger log = Logger.getLogger(Constants.class);

	/**
	 * 工程根目录
	 */
	public final static String PROJECT_PATH;
	/**
	 * Jetty工作目录
	 */
	public final static String WORKING_PATH;
	/**
	 * 临时classpath
	 */
	public final static String CLASSPATH;

	public final static int DEFAULT_PORT;
	public final static String PROJECT_NAME;
	/**
	 * 打包时被过滤的第三方依赖包
	 */
	public final static String PROJECT_DEPENDENCY_FILTER;
	public final static LayerEnum SYSTEM_LAYER[] = new LayerEnum[] {
			LayerEnum.WEB, LayerEnum.BIZ, LayerEnum.CORE, LayerEnum.COMMON };

	static {
		// project path
		PROJECT_PATH = new File(WebInit.class.getResource("/").getPath())
				.getParentFile().getParentFile().getParentFile().getParent()
				+ File.separator;
		log.info("Project path is: " + PROJECT_PATH);

		/**
		 * 资源文件路径
		 */
		String resourcesPath = "src" + File.separator + "main" + File.separator
				+ "resources" + File.separator;

		/**
		 * Log4J
		 */
		File f = new File(resourcesPath + "log4j.properties");
		if (!f.exists()) {
			System.err.println(f.getAbsolutePath() + " not exist.");
			System.exit(0);
		}

		PropertyConfigurator.configure(f.getAbsolutePath());
		log.info("Config log4j: " + f.getAbsolutePath());

		Properties p = new Properties();
		/**
		 * Load config file
		 */
		f = new File(resourcesPath + "test-config.properties");

		try {
			p.load(new FileInputStream(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			log.error(f.getAbsolutePath() + " not found.");
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
			log.error("Read file \"" + f.getAbsolutePath() + "\" error.");
			System.exit(0);
		}

		String docs = p.getProperty("jpxx.base.path");
		if (docs == null) {
			log.error("Error! Need config [jpxx.base.path]");
			System.exit(-1);
		}

		// working path
		WORKING_PATH = PROJECT_PATH + File.separator + docs + File.separator
				+ "htdocs" + File.separator;
		log.info("Jetty working path is: " + WORKING_PATH);

		// class path
		CLASSPATH = System.getProperty("user.home") + File.separator + ".jetty"
				+ File.separator + ".classpath" + File.separator;
		log.info("Project temp classpath is: " + CLASSPATH);

		// server port
		int port = 80;
		try {
			port = Integer.parseInt(p.getProperty("jpxx.port"));

			if (port < 1 || port > 65535) {
				log.error("[jpxx.port] must between 1-65535");
				port = 80;
				log.info("Jetty use default port=" + port);
			}
		} catch (Exception e) {
			log.error("[jpxx.port] must be a integer.");
			log.info("Jetty use default port=" + port);
		}
		DEFAULT_PORT = port;

		// project name
		PROJECT_NAME = p.getProperty("jpxx.project.name");
		if (PROJECT_NAME == null) {
			log.error("Error! Need config [jpxx.project.name]");
			System.exit(0);
		} else {
			log.info("Current project is: " + PROJECT_NAME);
		}

		PROJECT_DEPENDENCY_FILTER = p
				.getProperty("jpxx.project.dependency.jar.filter");
	}

	public final static String SRC_JAVA_PATH = "src" + File.separator + "main"
			+ File.separator + "java" + File.separator;
	public final static String SRC_RESOURCES_PATH = "src" + File.separator
			+ "main" + File.separator + "resources" + File.separator;
	/**
	 * 该常量主要用于控制是否在启动测试服务器时打包WAR，如果想加快测试服务器的 启动速度，请将此变量设置为false。
	 */
	public final static boolean CREATE_WAR = true;
}
